package 数组.middle;

import java.util.Arrays;

/**
 * @author Lw中
 * @date 2020/9/8 9:35
 */
public class Mst0107 {

    /**
     * 旋转矩阵
     * @param matrix
     */

    public static void rotate(int[][] matrix) {
        int length = matrix.length;
        // 一：先按对角线进行翻转
        for (int i = 0; i < length; i++) {
            for (int j = i; j < length; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.println(Arrays.toString(matrix[i]));
        }
        // 二：再按中轴线进行翻转
        // 1.计算中轴线位置
        int mid = length / 2;
        // 2.在循环体中进行翻转
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < mid; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][length - 1 - j];
                matrix[i][length - 1 - j] = temp;
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.println(Arrays.toString(matrix[i]));
        }
    }

    public static void main(String[] args) {
        int[][] nums = {{1,2,3}, {4,5,6}, {7,8,9}};
        rotate(nums);
    }

}
